package com.pingan.haofang.searchcloud.web.users.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.pingan.haofang.gov.sm.account.common.JsonResult;
import com.pingan.haofang.gov.sm.account.common.UserThreadContext;
import com.pingan.haofang.gov.sm.account.common.annotation.NoLogin;
import com.pingan.haofang.gov.sm.account.entity.domain.Account;
import com.pingan.haofang.gov.sm.account.entity.domain.AccountInfo;
import com.pingan.haofang.gov.sm.account.entity.dto.account.AccountInfoDTO;
import com.pingan.haofang.gov.sm.account.entity.dto.account.CodeRegisterDTO;
import com.pingan.haofang.gov.sm.account.entity.enums.FaceValidateStatusEnum;
import com.pingan.haofang.gov.sm.account.entity.enums.NameValidateStatusEnum;
import com.pingan.haofang.gov.sm.account.entity.vo.AccountVO;
import com.pingan.haofang.gov.sm.account.service.account.IAccountInfoService;
import com.pingan.haofang.gov.sm.account.service.account.IAccountService;
import com.pingan.haofang.gov.sm.account.service.account.facade.AccountTransfer;
import com.pingan.haofang.searchcloud.common.constants.WebConstants;
import com.pingan.haofang.searchcloud.web.users.form.LoginForm;
import com.pingan.haofang.searchcloud.web.users.form.RegisterAccountForm;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 用户登陆
 *
 * @author zhangbi617
 * @date 02/05/2017
 */
@RestController
@RequestMapping(value = WebConstants.WEB_PREFIX + "/auth")
@Api(tags = "System.Login", description = "用户登陆登出")
public class CurrentLoginController {
	@Autowired
	private IAccountService accountService;

	@Autowired
	private IAccountInfoService accountInfoService;

	@Autowired
	private AccountTransfer accountTransfer;

	/**
	 * 登陆
	 *
	 * @param request
	 */
	@NoLogin
	@ApiOperation(value = "用户登陆")
	@PostMapping("/login")
	public void login(@RequestBody @Valid LoginForm loginForm, HttpServletRequest request) {
		accountService.login(loginForm.getUserName(), loginForm.getPassword(), 2);
	}

	/**
	 * 登出
	 *
	 * @param request 请求
	 * @return
	 */
	@ApiOperation(value = "用户登出")
	@NoLogin
	@GetMapping("/logout")
	public void logout(HttpServletRequest request) {
		request.getSession().invalidate();
		UserThreadContext.putSessionVisitor(null);
	}

	@ApiOperation(value = "手机号注册")
	@NoLogin
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public JsonResult<AccountVO> register(@RequestBody RegisterAccountForm form) {
		CodeRegisterDTO dto = new CodeRegisterDTO();
		dto.setSourceType(2);
		dto.setMobile(form.getMobile());
		dto.setPassword(form.getPassword());
		dto.setCheckPassword(form.getPassword());
		dto.setMsgAuthCode(form.getMsgAuthCode());

		Account account = accountService.codeRegister(dto);
		AccountInfoDTO accountInfoDTO = new AccountInfoDTO();
		accountInfoDTO.setAccountId(account.getId());
		accountInfoDTO.setEmail(form.getEmail());
		accountInfoDTO.setFaceVerificationStatus(FaceValidateStatusEnum.PASS.getValue());
		accountInfoDTO.setNameVerificationStatus(NameValidateStatusEnum.PASS.getValue());
		AccountInfo accountInfo = accountInfoService.addAccountInfo(accountInfoDTO);
		AccountVO result = accountTransfer.toAccountVO(account, accountInfo);
		return JsonResult.successResponse(result);
	}

}
